package com.my.lock.v11;

import java.util.concurrent.atomic.AtomicInteger;

public class Semaphore {
    private AtomicInteger semaphore;
    private int max;

    public Semaphore(int max) {
        this.max = max;
        this.semaphore = new AtomicInteger(max);
    }

    public boolean increment() {
        int value = this.semaphore.get();
        return value < this.max && this.semaphore.compareAndSet(value, value + 1);
    }

    public boolean decrement() {
        int value = this.semaphore.get();
        return value > 0 && this.semaphore.compareAndSet(value, value - 1);
    }

    public AtomicInteger getSemaphore() {
        return semaphore;
    }

    public int getMax() {
        return max;
    }
}
